// File Name: key_ctrl.c
// Author: kukukukuku~
// Date: 2024-12-24

#include "apm32f00x_tmr1.h"
#include "FreeRTOS.h"
#include "task.h"
#include "./Key/inc/key_ctrl.h"

/**
 * @brief   Key毫秒级延时
 * @param   ms: 延时时间
 * @retval  None
 * @note    None
*/
void key_delay_ms(uint16_t ms)
{
    if(ms == 0)
        return;
    
    vTaskDelay(ms);
}


/**
 * @brief   Key获取电平
 * @param   None
 * @retval  电平
 * @note    None
*/
uint8_t key_polarity_get(KEY_GPIO_OBJECT_T * const gpio)
{
    return gpio == NULL ? 0 : GPIO_ReadInputBit(gpio->port, gpio->pin);
}


/**
 * @brief   Key开启定时器1
 * @param   None
 * @retval  None
 * @note    None
*/
void key_tmr1_enable(void)
{
    //只有当定时器关闭时才开启
    if(KEY_TMR->CTRL1_B.CNTEN == BIT_RESET)
        TMR1_Enable(KEY_TMR);
}


/**
 * @brief   Key关闭定时器1
 * @param   None
 * @retval  None
 * @note    None
*/
void key_tmr1_disable(void)
{
    //关闭定时器并清除计数器
    TMR1_Disable(KEY_TMR);
    TMR1_SetCounter(KEY_TMR, 0);
}
